import { Toast } from "antd-mobile"
import { http } from "../../utils/http"
// 获取文章详情
export const getArticleDetail = (id) => async (dispatch) => {
  const { data } = await http.get(`articles/${id}`)
  dispatch({ type: "GET_ACTICLEDETAIL", payload: data.data })
}
// 对文章点赞
export const LiveActive = (id, attitude) => async (dispatch) => {
  // payload为1是取消点赞,否者是点赞
  if (attitude === 1) {
    await http.delete(`article/likings/${id}`)
  } else {
    await http.post("/article/likings", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_ARTICLE",
    payload: {
      name: "attitude",
      value: attitude === 1 ? 0 : 1
    }
  })
}

// 收藏文章
export const collecteActive = (id, is_collected) => async (dispatch) => {
  // is_collected为真则取消收藏,否者是收藏
  if (is_collected) {
    await http.delete(`article/collections/${id}`)
  } else {
    await http.post("/article/collections", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_ARTICLE",
    payload: {
      name: "is_collected",
      value: !is_collected
    }
  })
}
// 获取文章评论
export const getCommentList = (payload) => async (dispatch) => {
  const { type, source } = payload
  const { data } = await http.get("/comments", { params: { type, source } })
  if (type === "a") {
    // 保存文章评论
    dispatch({ type: "GET_COMMENT", payload: data.data })
  } else {
    // 保存文章评论回复
    dispatch({ type: "GET_REPLY", payload: data.data })
  }
}
// 评论文章
export const pubilshArticle = (id, value) => async (dispatch) => {
  await http.post("/comments", {
    target: id,
    content: value,
    art_id: id
  })
  // 重新获取文章评论
  dispatch(getCommentList({ type: "a", source: id }))
}
// 对评论点赞
export const handleLikeComment = (id, isLiking) => async (dispatch) => {
  // isLiking 为ture 则是取消点赞
  if (isLiking) {
    await http.delete(`comment/likings/${id}`)
  } else {
    await http.post("/comment/likings", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_COMMENT",
    payload: {
      id: id,
      isLiking: !isLiking
    }
  })
}
// 回复评论
export const publishReply =
  (commId, replyValue, articleId) => async (dispatch) => {
    const { data } = await http.post("/comments", {
      target: commId,
      content: replyValue,
      art_id: articleId
    })
    if (data.message === "OK") {
      Toast.show({
        icon: "success",
        content: "回复成功"
      })
    } else {
      Toast.show({
        icon: "fail",
        content: "回复失败"
      })
    }
    // 重新获取文章评论回复
    dispatch(getCommentList({ type: "c", source: commId }))
  }

export const likeReply = (id, liking) => async (dispatch) => {
  // isLiking 为ture 则是取消点赞
  if (liking) {
    await http.delete(`comment/likings/${id}`)
  } else {
    await http.post("/comment/likings", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_REPLY",
    payload: {
      rid: id,
      liking: !liking
    }
  })
}
// user/followings
export const Follow = (id, is_followed) => async (dispatch) => {
  // is_followed为真是取消关注
  if (is_followed) {
    await http.delete(`/user/followings/${id}`)
  } else {
    await http.post("/user/followings", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_ARTICLE",
    payload: {
      name: "is_followed",
      value: !is_followed
    }
  })
}
export const CommentFollow = (id, is_followed) => async (dispatch) => {
  // is_followed为真是取消关注
  if (is_followed) {
    await http.delete(`/user/followings/${id}`)
  } else {
    await http.post("/user/followings", {
      target: id
    })
  }
  dispatch({
    type: "UPDATE_FOLLOW",
    payload: {
      commId: id,
      comName: "is_followed",
      comValue: !is_followed
    }
  })
}
